package com.maiji.cloud.controller.shopingmall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.maiji.cloud.controller.BaseController;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.ReduceWeightFundLogReqData;
import com.maiji.cloud.request.shopingmall.ApplayRefundReqDto;
import com.maiji.cloud.request.shopingmall.BalancePayReqData;
import com.maiji.cloud.request.shopingmall.CapitalIOListReqData;
import com.maiji.cloud.request.shopingmall.IntegralGainFlowReqData;
import com.maiji.cloud.request.shopingmall.OrderReturnMoneyFlowReqData;
import com.maiji.cloud.request.shopingmall.PayResDto;
import com.maiji.cloud.request.shopingmall.UserApplayRefundReqDto;
import com.maiji.cloud.request.shopingmall.UserBindWinthdrawAccountResData;
import com.maiji.cloud.request.shopingmall.WXRefundReqDto;
import com.maiji.cloud.request.shopingmall.WeixinPayReqDto;
import com.maiji.cloud.request.shopingmall.WithdrawReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.login.ReduceWeightFundLogResData;
import com.maiji.cloud.response.shopingmall.AliPayResData;
import com.maiji.cloud.response.shopingmall.BindWinthdrawAccountResData;
import com.maiji.cloud.response.shopingmall.CapitalIOListResDto;
import com.maiji.cloud.response.shopingmall.CapitalInfoResData;
import com.maiji.cloud.response.shopingmall.FinancialDetailsResData;
import com.maiji.cloud.response.shopingmall.FinancialDetailsResDto;
import com.maiji.cloud.response.shopingmall.IntegralFlowResDto;
import com.maiji.cloud.response.shopingmall.OrderReturnMoneyFlowResData;
import com.maiji.cloud.utils.CommonIntValueEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * Description: 资金相关
 * 
 * @author zhanglr
 * @date 2019年7月5日
 */
@RestController
@Api(tags = {"资金"})
@RequestMapping("/capitalInteface/")
public class CapitalController extends BaseController {
	Logger logger = LoggerFactory.getLogger(CapitalController.class);
	
	
	
	/**
	 * 获得减肥基金明细
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getReduceWeightFundLogList")
	@ApiOperation(value = "减肥基金获取明细列表")
	public BaseDataResDto<List<ReduceWeightFundLogResData>> getReduceWeightFundLogList(
			@RequestBody BaseDataReqDto<ReduceWeightFundLogReqData> param,
			@RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken, REST_URL_APPLOGIN + "/user/getReduceWeightFundLogList", param,
				BaseDataResDto.class);
	}
	
	/**
	 * 订单返现收入明细
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getOrderReturnMoneyFlow")
	@ApiOperation(value = "订单返现收入明细")
	public BaseDataResDto<List<OrderReturnMoneyFlowResData>> getOrderReturnMoneyFlow(@RequestBody BaseDataReqDto<OrderReturnMoneyFlowReqData> param, @RequestHeader("maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/getOrderReturnMoneyFlow", param, BaseDataResDto.class);
		
	} 

	/**
	 * 减肥基金使用明细
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@ApiOperation(value = "减肥基金使用明细")
	@PostMapping("reduceWeightFundConsumeFlow")
	public BaseDataResDto<List<FinancialDetailsResData>> reduceWeightFundConsumeFlow(@RequestBody ReqMetaData param, @RequestHeader("maijiToken") String maijiToken){
		logger.info("CapitalController.reduceWeightFundConsumeFlow,param is {},maijiToken is {}", JSON.toJSONString(param),
				maijiToken);
		
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/reduceWeightFundConsumeFlow", param, BaseDataResDto.class);
	}
	
	/**
	 * 获取用户的资金信息
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getCapitalInfo")
    @ApiOperation(value = "获取用户的资金信息")
	public BaseDataResDto<CapitalInfoResData>  getCapitalInfo(@RequestHeader(name = "maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/getCapitalInfo", null, BaseDataResDto.class);
	}
	
	@PostMapping("financialDetails")
	@ApiOperation(value = "现金流水列表")
	public FinancialDetailsResDto financialDetails(@RequestBody ReqMetaData param ,@RequestHeader("maijiToken") String maijiToken) {

		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/financialDetails", param, FinancialDetailsResDto.class);
	}
	
	
	/**
	 * 积分消费流水
	 * @return
	 */
	@PostMapping("integralConsumeFlow")
	@ApiOperation(value = "积分消费流水")
	public IntegralFlowResDto integralConsumeFlow(@RequestBody ReqMetaData param, @RequestHeader("maijiToken") String maijiToken){
		logger.info("CapitalController.integralConsumeFlow,param is {},maijiToken is {}", JSON.toJSONString(param),
				maijiToken);
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/integralConsumeFlow", param, IntegralFlowResDto.class);
	}
	
	/**
	 * 积分获得流水
	 * @return
	 */
	@PostMapping("integralAddFlow")
	@ApiOperation(value = "积分增加流水")
	public IntegralFlowResDto integralGainFlow(@RequestBody BaseDataReqDto<IntegralGainFlowReqData> param, @RequestHeader("maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/integralAddFlow", param, IntegralFlowResDto.class);
	}

	/**
	 * 获得分销账户提现记录列表
	 * 
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getDistributionCapitalIOList")
	@ApiOperation(value = "获得分销账户提现记录列表")
	public CapitalIOListResDto getDistributionCapitalIOList(@RequestBody BaseDataReqDto<CapitalIOListReqData> param, @RequestHeader("maijiToken") String maijiToken) {
		param.getData().setType(CommonIntValueEnum.WITHDRAWDISTRIBUTION.getValue()+"");
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/getcapitalIOList", param, CapitalIOListResDto.class);
	}
	
	/**
	 * 用户申请退款
	 * @return
	 */
	@PostMapping("userApplayRefund")
	@ApiOperation(value = "用户申请退款")
	public BaseResDto userApplayRefund(@RequestBody UserApplayRefundReqDto param,@RequestHeader(name = "maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/userApplayRefund", param, BaseResDto.class);
	}

	/**
	 * 用户撤销退款
	 * @return
	 */
	@PostMapping("userCancelRefund")
	@ApiOperation(value = "用户撤销退款")
	public BaseResDto userCancelRefund(@RequestBody BaseDataReqDto<String> baseDataReqDto,@RequestHeader(name = "maijiToken") String maijiToken) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/userCancelRefund", baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 微信支付成功回调
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@PostMapping("weixinPayCallBack")
	public String weixinPayCallBack(@RequestBody PayResDto param) {
		return postForObjectWithToken(null,REST_URL_SHOPINGMALL + "/capital/weixinPayCallBack", param, String.class);
	}

	/**
	 *  微信预支付
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("weixinPay")
	@ApiOperation(value = "微信预支付")
	public BaseDataResDto<Map<String,Object>> weixinPay(@RequestBody WeixinPayReqDto param,@RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/weixinPay", param, BaseDataResDto.class);
	}

	/**
	 *  微信小程序预支付
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("weixiAppletPay")
	@ApiOperation(value = " 微信小程序预支付")
	public BaseDataResDto<Map<String,Object>> weixiAppletPay(@RequestBody WeixinPayReqDto param,
			@RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/weixiAppletPay", param, BaseDataResDto.class);
	}
	
	/**
	 * 余额支付
	 * @param param
	 * @return
	 */
	@PostMapping("balancePay")
    @ApiOperation(value = "余额支付")
	public BaseDataResDto<String> balancePay(@RequestBody BaseDataReqDto<BalancePayReqData> param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("CapitalController.balancePay,param is {}",JSON.toJSONString(param));
		
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/balancePay", param, BaseDataResDto.class);
	}
	
	/**
	 * 余额消费明细
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("balanceConsumeFlow")
	@ApiOperation(value = "余额消费明细")
	public BaseDataResDto<List<FinancialDetailsResData>> balanceConsumeFlow(@RequestBody ReqMetaData param, @RequestHeader("maijiToken") String maijiToken){

		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/balanceConsumeFlow", param, BaseDataResDto.class);
	}
	
	
	
	/**
	 * 退款成功回调接口
	 * @throws Exception 
	 */
	@PostMapping("refundCallBack")
	public String refundCallBack(@RequestBody WXRefundReqDto param){
		return postForObjectWithToken(null,REST_URL_SHOPINGMALL + "/capital/refundCallBack", param, String.class);
	}

	/**
	 * 客服同意申请退款
	 * @return
	 */
	@PostMapping("agreeApplayRefund")
	public BaseResDto agreeApplayRefund(@RequestBody UserApplayRefundReqDto param,@RequestHeader(name = "maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/refundCallBack", param, BaseResDto.class);
	}

	/**
	 * 平台给用户退款专用接口
	 * @param param
	 * @param maijiToken
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("applayRefund")
	public BaseResDto applayRefund(@RequestBody ApplayRefundReqDto param,@RequestHeader(name = "maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/applayRefund", param, BaseResDto.class);
	}
	
	/**
	 * 支付宝支付
	 * @param param
	 * @param maijiToken
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("alipay")
	@ApiOperation(value = "支付宝支付")
    public BaseDataResDto<AliPayResData> alipay(@RequestBody WeixinPayReqDto param,@RequestHeader("maijiToken") String maijiToken){
		
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/alipay", param, BaseDataResDto.class);
	}

    /**
     * 支付宝支付成功回调
     * @return
     * @throws Exception 
     */
	@PostMapping("alipayCallBack")
    @ApiOperation(value = "支付宝支付成功回调")
    public BaseDataResDto<Map<String,Object>> alipayCallBack(HttpServletRequest request, HttpServletResponse response){
		//接收参数进行校验
        Map<String, String> parameters = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
            String key = entry.getKey();
            String[] values = entry.getValue();
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            parameters.put(key, valueStr);
        }
        logger.info("alipayCallBack parameters is parameters={}, parameters is {}", parameters,parameters);
      return postForObjectWithToken(null,REST_URL_SHOPINGMALL + "/capital/alipayCallBack", parameters, BaseDataResDto.class);
    }
	/**
	 * 提现
	 * @param param
	 * @param maijiToken
	 * @return
	 * @throws Exception
	 */
	@PostMapping("withdraw")
	@ApiOperation(value = "提现")
	public BaseResDto withdraw(@RequestBody BaseDataReqDto<WithdrawReqData> param,@RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/withdraw", param, BaseResDto.class);
	}
	
	
	/**
	 * 查看是否绑定支付宝/微信 1：支付宝，2：微信
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("isBindWinthdrawAccount")
	@ApiOperation(value = "查看是否绑定支付宝/微信 1：支付宝，2：微信")
	public BaseDataResDto<List<BindWinthdrawAccountResData>> isBindWinthdrawAccount(@RequestBody BaseDataReqDto<List<Integer>> param,@RequestHeader("maijiToken") String maijiToken){
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/isBindWinthdrawAccount", param, BaseDataResDto.class);
	}
	/**
	 * 绑定提现账号
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("userBindWinthdrawAccount")
	@ApiOperation(value = "绑定提现账号")
	public BaseResDto userBindWinthdrawAccount(@RequestBody BaseDataReqDto<UserBindWinthdrawAccountResData> param,@RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/userBindWinthdrawAccount", param, BaseResDto.class);
	}
	
}
